package ch.usi.inf.pf2.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.usi.inf.pf2.alu.IdentifierType;

public class SpreadSheetFormulaWindow
{
    private JFrame formulaFrame;
    private JPanel panel;
    private JSpreadSheetGUI spreadTable;

    public SpreadSheetFormulaWindow(JSpreadSheetGUI spreadTable)
    {
	   this.spreadTable = spreadTable;
	   makeFrame();
    }

    public void makeFrame()
    {
	   formulaFrame = new JFrame("Formulas smart selection");
	   formulaFrame.setLocationRelativeTo(spreadTable.getFrame());
	   formulaFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	   formulaFrame.getContentPane().setLayout(new BorderLayout());
	   panel = new JPanel();
	   panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
	   makeDropDown();
	   formulaFrame.getContentPane().add(panel, BorderLayout.CENTER);
	   formulaFrame.pack();
	   formulaFrame.setVisible(true);
    }

    public void makeDropDown()
    {
	   String[][] formula = createFormulaString();
	   final String[] values = new String[formula.length];
	   for (int i = 0; i < formula.length; i++)
		  values[i] = formula[i][0];
	   final String[] comments = new String[formula.length];
	   for (int i = 0; i < formula.length; i++)
		  comments[i] = formula[i][1];

	   final JComboBox combo = new JComboBox(values);
	   combo.setSelectedIndex(0);
	   panel.add(combo);

	   final JLabel com = new JLabel(comments[combo.getSelectedIndex()]);
	   panel.add(com);

	   combo.addActionListener(new ActionListener()
	   {
		  @Override
		  public void actionPerformed(ActionEvent e)
		  {
			 com.setText(comments[combo.getSelectedIndex()]);
		  }
	   });

	   final JButton ok = new JButton("Insert");
	   panel.add(ok);
	   ok.addActionListener(new ActionListener()
	   {

		  @Override
		  public void actionPerformed(ActionEvent e)
		  {
			 int row = spreadTable.getSelectedRow()+1;
			 int col = spreadTable.getSelectedColumn()+1;
			 String s = "=" + (String) combo.getSelectedItem() + "()";
			 spreadTable.getSheet().insertDataAtCellForInit(s, col, row);
			 SpreadSheetTableModel tb = (SpreadSheetTableModel) spreadTable
				    .getTable().getModel();
			 tb.fireTableCellUpdated(spreadTable.getSelectedRow() + 1,
				    spreadTable.getSelectedColumn() + 1);
			 spreadTable.getSheet().setHasBeenSaved(false);
			 spreadTable.getFrame().repaint();
			 formulaFrame.dispose();
		  }
	   });

    }

    private String[][] createFormulaString()
    {
	   IdentifierType[] it = IdentifierType.values();
	   String[][] s = new String[it.length][2];
	   for (int i = 0; i < it.length; i++)
	   {
		  if (it[i] == IdentifierType.AVERAGE)
		  {
			 s[i][0] = "Average";
			 s[i][1] = "Compute the average among the selected cells";
		  } else if (it[i] == IdentifierType.SUM)
		  {
			 s[i][0] = "SUM";
			 s[i][1] = "Compute the sum among the selected cells";
		  } else if (it[i] == IdentifierType.LN)
		  {
			 s[i][0] = "ln";
			 s[i][1] = "Compute the natural logarithm of a number";
		  } else if (it[i] == IdentifierType.LOG)
		  {
			 s[i][0] = "log";
			 s[i][1] = "Compute the base 10 logarithm of a number";
		  } else if (it[i] == IdentifierType.MAX)
		  {
			 s[i][0] = "MAX";
			 s[i][1] = "Compute the maximum number among the selected cells";
		  } else if (it[i] == IdentifierType.MIN)
		  {
			 s[i][0] = "min";
			 s[i][1] = "Compute the minimum number among the selected cells";
		  } else if (it[i] == IdentifierType.SIN)
		  {
			 s[i][0] = "sin";
			 s[i][1] = "Compute the sine of a number";
		  } else if (it[i] == IdentifierType.COS)
		  {
			 s[i][0] = "cos";
			 s[i][1] = "Compute the cosine of a number";
		  } else if (it[i] == IdentifierType.COT)
		  {
			 s[i][0] = "cot";
			 s[i][1] = "Compute the co-tangent of a number";
		  } else if (it[i] == IdentifierType.TAN)
		  {
			 s[i][0] = "tan";
			 s[i][1] = "Compute the tangent of a number";
		  } else
			 ;
	   }

	   return s;
    }
}
